﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Numerics;

namespace dst
{
    class HTDup<T> where T : Exp
    {
        const int Max_Cap = 100000000;
        protected Dictionary<BigInteger, T> dic = new Dictionary<BigInteger, T>();
        protected Dictionary<BigInteger, T> dicBase = new Dictionary<BigInteger, T>();
        int count = 0;

        public Exp Add(BigInteger key, T val)
        {

            lock (this)
            {
                count++;
                if (count % Max_Cap == 0)
                {
                    Console.WriteLine("----------------GC Performed--------------------");
                    dic.Clear();
                    for (Dictionary<BigInteger, T>.Enumerator enu = dicBase.GetEnumerator(); enu.MoveNext(); )
                        dic.Add(enu.Current.Key, enu.Current.Value);
                }

                if ((val is Ele) && !dicBase.ContainsKey(key))
                    dicBase.Add(key, val);
                    

                if (!dic.ContainsKey(key))
                {
                    dic.Add(key, val);
                }
                else
                    val = dic[key];

                return val;
            }
        }

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("\n");
            sb.Append("----------------Simplification Table--------------------\n");

            Dictionary<BigInteger, T>.Enumerator e = dic.GetEnumerator();
            while (e.MoveNext())
            {
                sb.Append("|>> ");
                sb.Append(e.Current.Key + "\n");
                sb.Append("|   ");
                sb.Append(e.Current.Value.GetType() + "\t");
                sb.Append(e.Current.Value + "\n");
                sb.Append("|\n");
            }
            sb.Append("---------------------------------------------------------\n");

            return sb.ToString();
        }
    }
}
